/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.agentes;

import co.edu.unal.behaviours.CheckStockServer;
import co.edu.unal.behaviours.CreateStock;
import co.edu.unal.behaviours.OfferRequestsServer;
import co.edu.unal.behaviours.PurchaseOrdersServer;
import co.edu.unal.gui.BookManufacturerGui;
import co.edu.unal.inventory.Stock;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import javax.swing.JList;

/**
 *
 * @author Camilo
 */
public class BookManufacturerAgent extends Agent{
    private HashMap catalogue;
    private BookManufacturerGui myGui;
    private Stock stock;
    @Override
    protected void setup() {
        //Creando gui
        myGui=new BookManufacturerGui(this);
        myGui.showGui();
        //Creando inventario inicial
        stock = new Stock(100, 10, 1);
        addBehaviour(new CreateStock(stock, myGui.getLog()));
        // Printout a welcome message
        myGui.getLog().setText("Hello! Manufacturer-agent " + getAID().getLocalName() + " is ready.");
        //Agregar libro al catalogo
        catalogue = new HashMap();
        Random r=new Random(new Date().getTime());
        catalogue.put("Estudio en escarlata", 50+r.nextInt(30));
        //Agregar comportamientos
        addBehaviour(new CheckStockServer(catalogue, stock,myGui.getLog(),50+r.nextInt(30),"book-manufacturer",myGui.getLista()));
        addBehaviour(new OfferRequestsServer(catalogue));
        addBehaviour(new PurchaseOrdersServer(catalogue,"book-manufacturer",myGui.getLog()));
        // Register the book-selling service in the yellow pages
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType("book-manufacturer");
        sd.setName("JADE-book-trading");
        dfd.addServices(sd);
        try {
            DFService.register(this, dfd);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }
    }
    
    @Override
    protected void takeDown() {
        // Deregister from the yellow pages
        try {
            DFService.deregister(this);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }
// Printout a dismissal message
        myGui.getLog().setText(myGui.getLog()+"\n"+"Seller-agent " + getAID().getLocalName() + " terminating.");
    }

    public Stock getInventario() {
        return stock;
    }

    public void setInventario(Stock inventario) {
        this.stock = inventario;
    }

    private void addBehaviour(CheckStockServer checkStockServer, JList lista) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
